package com.user.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.dao.SysRoleDao;
import com.user.dao.SysUserRoleDao;
import com.user.entity.SysRole;
import com.user.entity.SysUserRole;
import com.user.service.IRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: Member_Perks_Store
 * @description:
 * @author: 无恙
 * @create: 2024-06-17 09:21
 **/

@Service
public class IRoleImpl extends ServiceImpl<SysRoleDao, SysRole> implements IRole {

    private SysRoleDao roleDao;

    private SysUserRoleDao userRoleDao;
    @Autowired
    public void setUserRoleDao(SysUserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }

    @Autowired
    public void setSysRoleMapper(SysRoleDao roleDao) {
        this.roleDao = roleDao;
    }
    @Override
    public List<SysRole> getByUid(Long uid) {

        //获取当前用户角色
        List<SysUserRole> userId = userRoleDao.selectByMap(Collections.singletonMap("user_id", uid));

        //获取角色
        return roleDao.selectBatchIds(userId.stream().
                map(SysUserRole::getRoleId)
                .collect(Collectors.toList()));
    }

    @Override
    public List<String> getRoleIdByUid(Long uid) {
        return this.getByUid(uid).stream().map(SysRole::getRoleCode).collect(Collectors.toList());
    }

}
